package com.mallcai.bigdata.ladon.service.report.impl.handler;

import com.mallcai.bigdata.ladon.client.vo.ReportDataVO;
import com.mallcai.bigdata.ladon.datasource.AbstractJdbcHandler;
import com.mallcai.bigdata.ladon.datasource.HandlerFactory;
import com.mallcai.bigdata.ladon.datasource.RunInfoResult;
import com.mallcai.bigdata.ladon.dpl.entity.common.Datasource;
import com.mallcai.bigdata.ladon.dpl.entity.report.ReportSql;
import com.mallcai.bigdata.ladon.service.report.impl.ReportDataVoExtractor;
import com.mallcai.bigdata.ladon.service.report.impl.ReportQueryContext;
import com.mallcai.bigdata.ladon.service.report.impl.handler.support.MapParamPreparedStatementSetter;
import lombok.extern.slf4j.Slf4j;
import org.springframework.core.annotation.Order;
import org.springframework.stereotype.Service;

import java.util.Map;

/**
 * @author feixy
 * @version 1.0
 * @date 2019-07-20 08:37
 */
@Service
@Order
@Slf4j
public class ReportQueryDbHandler extends AbstractReportQueryPreHandler {

    @Override
    public void preInternal(ReportQueryContext context) {
        String sql = context.getSql();
        ReportSql reportSql = context.getReportSql();
        log.debug(sql);
        Datasource datasource = context.getDatasource();
        AbstractJdbcHandler handler = HandlerFactory.getHandler(context.getDatasource());
        RunInfoResult<ReportDataVO> runInfoResult = handler.queryDataWithRunInfo(datasource, sql,
                new MapParamPreparedStatementSetter(context.getSqlParams(),reportSql.getTimeout()),
                new ReportDataVoExtractor());
        context.setResult(runInfoResult);
    }


}
